{extend name='public/base' /}
{block name="css"}
<link href="/res/adminui/dist/css/login.css" rel="stylesheet">
<link href="/res/style/template.css" rel="stylesheet">
{/block}

{block name="content"}
<div class="layadmin-user-login layadmin-user-display-show" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>{$data['name']}管理后台</h2>
            <p>{$data['desc']}</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"></label>
                <input type="text" id="username" placeholder="用户名" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"></label>
                <input type="password" id="password" placeholder="密码" class="layui-input">
            </div>
            <input type="hidden" id="code" name="code">
            <input type="hidden" id="randStr" name="randStr">
            <div class="layui-form-item" id="loginButton">
                <button class="layui-btn layui-btn-fluid">登 入</button>
            </div>
            <div class="layui-trans layui-form-item layadmin-user-login-other">
                <label>登录</label>
                <!--                <a href="javascript:;"><i class="layui-icon layui-icon-login-qq"></i></a>-->
                <a href="javascript:;"><i class="layui-icon layui-icon-login-wechat"></i></a>
                <!--                <a href="javascript:;"><i class="layui-icon layui-icon-login-weibo"></i></a>-->
                <!--                <a href="reg.html" class="layadmin-user-jump-change layadmin-link">注册帐号</a>-->
            </div>
        </div>
    </div>
    <div class="layui-trans layadmin-user-login-footer">
        <p>© {$data['copyright']} <a href="//{$data['domain']}" target="_blank">{$data['domain']}</a></p>
        <p>
            <span>本站已勉强运行
                <b id="day">{$data['d']}</b>天
                <b id="hour">{$data['H']}</b>小时
                <b id="minute">{$data['i']}</b>分钟
                <b id="second">{$data['s']}</b>秒
            </span>
            <span>当前有
                <b class="people-number">0</b>
                人在线,共打开了
                <b class="page-number">0</b>
                个页面在浏览本网站
            </span>
        </p>
    </div>
</div>
{/block}

{block name="js"}
<script src="/res/vgt.js"></script>
<script type="text/javascript" src="/chat/swfobject.js"></script>
<script type="text/javascript" src="/chat/web_socket.js"></script>
<script>
    layui.use(['layer'], function () {
        let $ = layui.$,
            layer = layui.layer;
        initGeetest4({
            // 省略必须的配置参数
            product: 'bind',
            captchaId: 'a5c07815033e0ce05ce6c4a5a5379287',
        }, function (captchaObj) {
            captchaObj.onReady(function () {
                //验证码ready之后才能调用verify方法显示验证码
            }).onSuccess(function () {
                let result = captchaObj.getValidate();
                console.log(result);
                $.ajax({
                    type: "post",
                    url: '/accountVerify',
                    data: {
                        'lot_number': result.lot_number,
                        'captcha_output': result.captcha_output,
                        'pass_token': result.pass_token,
                        'gen_time': result.gen_time,
                        'username': $('#username').val(),
                        'password': $('#password').val(),
                    },
                    success: function (data) {
                        // 根据服务端二次验证的结果进行跳转等操作
                        if (data.code !== 0) {
                            layer.msg(data.message, {icon: 5});
                            captchaObj.reset(); // 调用该接口进行重置
                        } else {
                            location.href = '/manage/index';
                        }
                    }
                });
            }).onError(function () {
                //your code
            })
            // 按钮提交事件
            $("#loginButton").on('click', function () {
                if (!loginCheck()) {
                    return false;
                }
                captchaObj.showCaptcha(); //显示验证码
            });
            //回车事件
            document.onkeydown = function (e) {
                let ev = document.all ? window.event : e;
                if (ev.keyCode === 13) {
                    if (!loginCheck()) {
                        return false;
                    }
                    captchaObj.showCaptcha(); //显示验证码
                }
            }
            //二维码
            $('.layui-icon-login-wechat').on('click', function () {
                $.ajax({
                    type: "post",
                    url: '/WxScanCode',
                    data: {
                        'type': 1,
                    },
                    success: function (res) {
                        // 根据服务端二次验证的结果进行跳转等操作
                        if (res.code !== 0) {
                            layer.msg('获取二维码失败', {icon: 5});
                        } else {
                            // console.log(res.data.url);
                            layer.open({
                                type: 1,
                                title: false,
                                closeBtn: false,
                                area: ['300px', '300px'],
                                shade: 0.8,
                                id: 'LAY_layuipro',
                                // btnAlign: 'c',
                                moveType: 1,//拖拽模式，0或者1
                                content: '<img src="' + res.data.url + '" style="width: 300px;">',
                                shadeClose: true,
                                // time: 60000,
                                success: function (layero, index) {
                                    //定时查询扫码状态
                                    var number = 0,
                                        intervalBox = setInterval(function () {
                                            number++;
                                            if (number >= 60) {
                                                layer.msg('二维码已超时,请重新扫码！');
                                                clearInterval(intervalBox);//清除interval定时器
                                                layer.close(index);
                                            }
                                            $.ajax({
                                                type: "post",
                                                url: '/getCodeStatus',
                                                data: {
                                                    'id': res.data.id,
                                                },
                                                success: function (data) {
                                                    // 根据服务端二次验证的结果进行跳转等操作
                                                    if (data.code !== 0) {
                                                        layer.msg(data.message, {icon: 5});
                                                        clearInterval(intervalBox);
                                                        layer.close(index);
                                                    } else if (data.data.status === 1) {
                                                        location.href = '/manage/index';
                                                        clearInterval(intervalBox);
                                                    } else if (data.data.status !== 0) {
                                                        layer.msg(data.data.text, {icon: 5});
                                                        clearInterval(intervalBox);
                                                        layer.close(index);
                                                    }
                                                }
                                            });
                                        }, 2000);
                                    // clearInterval(intervalBox);//清除interval定时器
                                },
                            });
                        }
                    }
                });
            })
        });

        //登陆检测
        function loginCheck() {
            let username = $('#username').val(),
                password = $('#password').val();
            if (username.length < 1) {
                layer.msg('请输入用户名', {icon: 5});
                return false;
            }
            if (password.length < 1) {
                layer.msg('请输入密码', {icon: 5});
                return false;
            }
            return true;
        }

        window.setInterval(function () {
            let day = $("#day").html(),
                hour = $("#hour").html(),
                minute = $("#minute").html(),
                second = $("#second").html();
            if (second === '59') {
                minute = parseInt(minute) + 1;
                second = 0;
                if (minute === 59) {
                    hour = parseInt(hour) + 1;
                    minute = 0;
                    if (hour === 23) {
                        hour = 0;
                        day = parseInt(day) + 1;
                    }
                }
            } else {
                second = parseInt(second) + 1;
            }
            $('#day').html(day);
            $('#hour').html(hour);
            $('#minute').html(minute);
            $('#second').html(second);
        }, 1000);

        WEB_SOCKET_SWF_LOCATION = "/swf/WebSocketMain.swf";
        WEB_SOCKET_DEBUG = true;
        let ws;
        ws = new WebSocket("ws://182.61.26.102:7272");
        ws.onopen = function () {
            // alert("连接成功");
        };
        ws.onmessage = onmessage;

        function onmessage(e) {
            let data = JSON.parse(e.data);
            switch (data['type']) {
                case 'ping':
                    ws.send('{"type":"pong"}');
                    break;
                // 更新在线用户信息
                case 'update_online_count':
                    $('.people-number').html(data['user_number']);
                    $('.page-number').html(data['page_number']);
                    break;
                case 'init':
                    $.post('/updateCount', {client_id: data['client_id']}, function (res) {
                    });
                    break;
            }
        }
    })
</script>
{/block}